package com.xiaomo.gitee.datamanager.controller.admin;

import com.xiaomo.gitee.datamanager.common.result.Code;
import com.xiaomo.gitee.datamanager.common.result.Result;
import com.xiaomo.gitee.datamanager.entity.CClassInfo;
import com.xiaomo.gitee.datamanager.entity.ClassInfo;
import com.xiaomo.gitee.datamanager.service.category.ClassInfoService;
import com.xiaomo.gitee.datamanager.service.category.impl.ClassInfoServiceImpl;
import com.xiaomo.gitee.datamanager.util.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/api")
@Tag(name = "数据集及大分类", description = "管理员操作数据集和大分类数据")
@RequiredArgsConstructor
public class AdminCategoryController {

    private final ClassInfoService classInfoService;

    //@PostMapping("/saveCategory")
    @PostMapping("/parent-category")
    @PreAuthorize("hasAuthority('category_p_cd')")
    @Operation(
            summary = "创建大分类",
            description = "管理员",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "classInfo",
                            description = "大类别信息模型",
                            required = true,
                            schema = @Schema(
                                    implementation = ClassInfo.class
                            )
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "操作成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    ),
                    @ApiResponse(
                            responseCode = "40901",
                            description = "此id已存在",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    ),
                    @ApiResponse(
                            responseCode = "40902",
                            description = "此名称已存在",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> createParentCategory(@RequestBody ClassInfo classInfo) {
        return switch (classInfoService.insertParentClass(classInfo)) {
            case 0 -> Result.ok();
            case -1 -> Result.fail(40901, "此id已存在");
            case -2 -> Result.fail(40902, "此名称已存在");
            default -> Result.fail("发生未知错误");
        };
    }

    //@PostMapping("/saveChildCategory")
    @PostMapping("/child-category")
    @PreAuthorize("hasAuthority('category_c_cd')")
    @Operation(
            summary = "创建数据集",
            description = "管理员",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "cclassInfo",
                            description = "数据集信息模型",
                            required = true,
                            schema = @Schema(
                                    implementation = CClassInfo.class
                            )
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    ),
                    @ApiResponse(
                            responseCode = "409",
                            description = "此数据集名称在此大类别中已存在",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> createChildCategory(@RequestBody CClassInfo cclassInfo) {
        return switch (classInfoService.insertChildClass(cclassInfo)) {
            case 0 -> Result.ok();
            case -1 -> Result.fail(40901, "此数据集id已存在");
            case -2 -> Result.fail(409, "此数据集名称在此大类别中已存在");
            default -> Result.fail("发生未知错误");
        };
    }

    //@PostMapping("/deleteChildCategory")
    @DeleteMapping("/child-category")
    @PreAuthorize("hasAuthority('category_c_cd')")
    @Operation(
            summary = "删除数据集",
            description = "管理员通过数据集id删除数据集",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(name = "category",description = "数据集ID",required = true,content = @Content(mediaType = "application/json"))
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> deleteChildCategory(@RequestParam Long category) {
        classInfoService.deleteChildClass(category);
        return Result.ok();
    }

}
